<!-- 排班管理 -->
<template>
  <div class="dashboard-container transport-task customer-list-box">
    <el-card
      class="search-card"
      shadow="never"
    >
      <el-form
        ref="searchForm"
        :model="params"
        label-width="80px"
      >
        <div class="filter-container">
          <el-row :gutter="60">
            <el-col :span="8">
              <el-form-item
                label="员工账号:"
                style="margin-bottom: 0px"
                label-width="110px"
              >
                <el-input
                  v-model="params.employeeNumber"
                  placeholder="请输入司机账号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="员工姓名:"
                style="margin-bottom: 0px"
                label-width="110px"
              >
                <el-input
                  v-model="params.name"
                  placeholder="请输入员工姓名"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="工作模式:"
                style="margin-bottom: 20px"
                label-width="110px"
              >
                <el-select
                  v-model="params.workPatternId"
                  placeholder="请选择工作模式"
                >
                  <el-option
                    v-for="ele in workPatternInfo"
                    :key="ele.id"
                    :label="ele.name"
                    :value="ele.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="月份:"
                style="margin-bottom: 20px"
                label-width="110px"
              >
                <el-date-picker
                  v-model="params.month"
                  type="month"
                  placeholder="选择月份"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="所属机构:"
                style="margin-bottom: 20px"
                label-width="110px"
              >
                <treeselect
                  v-model="params.agentId"
                  :options="agencyOptions"
                  :normalizer="normalizer"
                  :searchable="true"
                  style="width: 100%"
                  placeholder="请选择所属机构"
                  no-options-text="暂无数据"
                  no-results-text="暂无数据"
                  @select="selectEndAgency"
                  @open="open"
                  @close="close"
                />
                <img
                  ref="arrows"
                  src="@/assets/pullDown.png"
                  style="
                      position: absolute;
                      width: 18px;
                      height: 18px;
                      right: 9px;
                      top: 12px;
                      cursor: pointer;
                      pointer-events: none;
                      transition-duration: 0.3s;
                    "
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button
                type="warning"
                @click="handleFilter"
              >搜索</el-button>
              <el-button
                class="reset-btn"
                plain
                @click="resetForm"
              >重置</el-button>
            </el-col>
          </el-row>
        </div>
      </el-form>
    </el-card>
    <el-card
      class="box-card"
      shadow="never"
    >
      <el-button
        type="primary"
        class="add-btn"
        @click="$router.push('workArrange-setting')"
      >排班设置</el-button>
      <el-button
        type="primary"
        plain
        class="add-btn"
        @click="bindWorkArrange"
      >绑定排班</el-button>
      <el-table
        class="waybill-table"
        :data="tableData"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="employeeNumber"
          label="员工账号"
          width="120"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="员工姓名"
          width="120"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="员工电话"
          width="140"
          fixed
        >
        </el-table-column>
        <el-table-column
          prop="userType"
          label="员工角色"
          width="120"
          fixed
          :formatter="userTypeFormatter"
        >
        </el-table-column>
        <el-table-column
          prop="workPatternName"
          label="当前工作模式"
          width="120"
          fixed
        >
        </el-table-column>
        <el-table-column
          v-for="k in day"
          :key="k"
        >
          <template
            slot="header"
          >
            <span>{{ k }}</span>
          </template>
          <template slot-scope="scoped">
            <el-tag
              v-if="scoped.row.workSchedules[k-1]"
              style="border-radius: 50%;"
            >上</el-tag>
            <el-tag
              v-else
              type="info"
              style="border-radius: 50%;"
            >休</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="params.page"
        :page-sizes="[5,10, 15, 20, 25]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="人工调整"
      :visible.sync="dialogVisible"
      destroy-on-close
      width="30%"
    >
      <el-radio-group
        v-model="workPatternType"
        class="workPatter"
        @input="changeInput"
      >
        <el-radio :label="1">礼拜制</el-radio>
        <el-radio :label="2">连续制</el-radio>
      </el-radio-group>
      <div class="modeName">
        <p>模式名称</p>
        <el-select
          v-model="workPatternId"
          class="nameChoose"
          placeholder="请选择"
        >
          <el-option
            v-for="item in dialogWorkPatternList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="updateWorkPattern"
        >确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>
<script>
import { workManage, selectWorkHistoryList, batchUpdateSet } from '@/api/transit'
import { agencyList } from '@/api/institutions'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: {
    Treeselect
  },
  data() {
    return {
      dialogVisible: false,
      agencyOptions: [],
      tableData: [],
      workPatternInfo: [],
      dialogWorkPatternList: [],
      carsList: [],
      total: 0,
      params: {
        page: 1,
        pageSize: 5,
        employeeNumber: '', // 员工账号
        name: '', // 员工姓名
        workPatternId: null, // 工作模式
        month: null, // 月份
        agentId: null// 机构ID
      },
      day: 30,
      multipleSelection: [],
      workPatternType: 1, // 工作模式类型
      workPatternId: null,
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.children
        }
      }
    }
  },
  created() {
    this.getAgencyList()
    this.getWorkingList()
    // this.getWorkArrangeList()
  },
  methods: {
    userTypeFormatter(row) {
      const MAP = {
        1: '员工',
        2: '快递员',
        3: '司机'
      }
      return MAP[row.userType]
    },
    // 人工调整排班
    async updateWorkPattern() {
      const userIdList = this.multipleSelection.map(ele => ele.id)
      console.log(userIdList)
      const data = {
        userIdList,
        userType: this.multipleSelection[0].userType,
        workPatternId: this.workPatternId,
        workPatternType: this.workPatternType
      }
      const res = await batchUpdateSet(data)
      if (res.code === 200) {
        this.$message.success('绑定成功')
        this.dialogVisible = false
      } else {
        this.$message.error(res.msg)
      }
    },
    changeInput(val) {
      this.workPatternId = null
      this.dialogWorkPatternList = this.workPatternInfo.filter(ele => ele.workPatternType === val)
    },
    bindWorkArrange() {
      if (this.multipleSelection.length > 0) {
        this.dialogVisible = true
      } else {
        this.$message.error('请选择调整人员')
      }
    },
    // 点击绑定排班获取选中用户数据
    handleSelectionChange(val) {
      this.multipleSelection = val
      console.log(this.multipleSelection)
    },
    // 获取工作模式下拉列表
    async getWorkingList() {
      const res = await selectWorkHistoryList()
      const obj = Object.values(res.data)
      this.workPatternInfo = [...obj[0], ...obj[1]]
      this.dialogWorkPatternList = this.workPatternInfo.filter(ele => ele.workPatternType === 1)
    },

    // 获取地机构列表数据
    async getAgencyList() {
      const { data: res } = await agencyList()
      // 获取数据给options
      this.agencyOptions = JSON.parse(res)
    },
    selectEndAgency(val) {
      this.params.agentId = val.id
    },
    open() {
      this.$refs.arrows.style.transform = 'rotate(-180deg)'
    },
    close() {
      this.$refs.arrows.style.transform = 'rotate(0deg)'
    },
    getDetail(id) {
      this.$router.push(`driver-detail?id=${id}`)
    },
    handleFilter() {
      this.getWorkArrangeList()
    },
    async resetForm() {
      await this.$refs.searchForm.resetFields()
      this.params.page = 1
      this.params.pageSize = 5
      this.params.employeeNumber = ''
      this.params.name = ''
      this.params.workPatternId = null
      this.params.month = null
      this.params.agentId = null
      this.day = 30
      this.getWorkArrangeList()
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.getWorkArrangeList()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getWorkArrangeList()
    },
    async getWorkArrangeList() {
      const res = await workManage(this.params)
      console.log(res)
      this.tableData = res.data.items
      this.day = this.tableData[0].workSchedules.length

      this.total = Number(res.data.counts)
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>

.workPatter{
  text-align: center;
}
.modeName{
  margin-top: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  .nameChoose{
    width: 300px;
    margin-left: 12px;
  }
}
::v-deep .el-dialog__body{
  text-align: center;
}
 ::v-deep .el-dialog__footer{
  text-align: center;
}
 ::v-deep .el-table tr td:last-child .cell {
  text-align: left ;
}
.add-btn{
  margin-bottom: 20px;
}
.el-pagination{
  padding: 20px 0;
  position: relative;
  left: 22%;
}
.search-card{
  margin: 8px 20px;
  padding: 20px 30px 0 30px;
  ::v-deep .el-card__body{
    padding: 0;
    .el-form-item__label{
      font-weight: 400;
    }
    .el-input__inner{
        width: 240px;
      }
  }
}
.cell{
    text-align: left;
    .edit{
      color: #419EFF;
    }
    .delete{
      color: #F56C6C;
    }
    .Partingline{
      color: #E3E3E3 ;
    }
}
.box-card{
  margin:  20px;
  padding: 30px;
  ::v-deep .el-card__body{
    padding: 0;
  }
}

::v-deep th.date{
  .cell{
    padding-left: 17px!important;
  }
}
.work {
  width: 24px;
  height: 24px;
  background: #ffeeeb;
  border: 1px solid #e15536;
  border-radius: 50%;
  text-align: center;
  color: #e15536;
  font-size: 13px;
}
.history-name {
  width: auto !important;
  ::v-deep .el-input__inner {
    width: 293px;
  }
}

.workArrange-manage-people-setting {
  ::v-deep .el-dialog__body {
    padding: 30px 50px 10px 50px !important;
  }
  ::v-deep .el-dialog__footer {
    padding-bottom: 40px;
  }
}

.work-type {
  ::v-deep .el-form-item__content {
    margin-left: 40px !important;
  }
  ::v-deep .el-radio:first-child {
    margin-right: 80px !important;
  }
  ::v-deep.el-radio__label {
    font-weight: bold;
    color: #333333;
  }
}

.dialog-footer {
  .el-button {
    width: 89px;
    height: 36px;
    padding: 10px 20px;
  }
}

.rest {
  width: 24px;
  height: 24px;
  background: #eff3f8;
  border: 1px solid #d8dde3;
  border-radius: 50%;
  text-align: center;
  color: #20232a;
  font-size: 13px;
}

.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 40px;
}
.transport-task ::v-deep .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

// 下拉框
::v-deep .el-select {
  width: 100%;
}

/*定义滚动条轨道 内阴影+圆角*/
::v-deep .el-table--scrollable-x .el-table__body-wrapper {
  padding-bottom: 5px;
  margin-bottom: 5px;
  &::-webkit-scrollbar {
    height: 10px;
  }

  /*定义滑块 内阴影+圆角*/
  &::-webkit-scrollbar-thumb {
    border-radius: 10px;
    background-color: rgba(144, 147, 153, 0.3);
  }

  &::-webkit-scrollbar-track-piece {
    margin-right: 3px;
    margin-left: 3px;
  }
}
::v-deep .el-card{
  overflow: initial;
}
</style>

<style></style>
